package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.mapper.AddressMapper;
import cn.jiahua.hospital.pojo.Address;
import cn.jiahua.hospital.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Address getAddressById(Integer id) {
        return addressMapper.getAddressById(id);
    }

    @Override
    public List<Address> getAddressListByMap(Map<String, Object> param) {
        return addressMapper.getAddressListByMap(param);
    }

    @Override
    public Integer getAddressCountByMap(Map<String, Object> param) {
        return addressMapper.getAddressCountByMap(param);
    }

    @Override
    public Integer qdtxAddAddress(Address address) {
        return addressMapper.insertAddress(address);
    }

    @Override
    public Integer qdtxModifyAddress(Address address) {
        return addressMapper.updateAddress(address);
    }

    @Override
    public Integer qdtxDeleteAddressById(Integer id) {
        return addressMapper.deleteAddressById(id);
    }

    @Override
    public Integer qdtxBatchDeleteAddress(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return addressMapper.batchDeleteAddress(param);
    }

    @Override
    public Page<Address> queryAddressPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = addressMapper.getAddressCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Address> addressList = addressMapper.getAddressListByMap(param);
        page.setList(addressList);
        return page;
    }
}